from django.db import models
from django.conf import settings
from apps.chefs.models import Chef

class BrowsingHistory(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='browsing_history'
    )
    chef = models.ForeignKey(
        Chef,
        on_delete=models.CASCADE,
        related_name='viewed_by'
    )
    viewed_at = models.DateTimeField(auto_now_add=True)
    
    class Meta:
        ordering = ['-viewed_at']
        verbose_name = '浏览历史'
        verbose_name_plural = '浏览历史'
        indexes = [
            models.Index(fields=['user', '-viewed_at']),
        ]

    def __str__(self):
        return f"{self.user.username} 浏览 {self.chef.name}"